package com.example.mall.web.controller;

import com.example.mall.domain.FreeShippingRules;
import com.example.mall.dto.FreeShippingRulesDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.FreeShippingRulesQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IFreeShippingRulesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/freeShippingRules")
public class FreeShippingRulesController {

    @Autowired
    public IFreeShippingRulesService freeShippingRulesService;

    /**
     * 分页查询包邮规则表信息
     * @param freeShippingRulesQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询包邮规则表信息",notes = "分页查询包邮规则表信息",httpMethod = "GET")
    public JSONResult queryPage(FreeShippingRulesQuery freeShippingRulesQuery){
        PageDTO<FreeShippingRulesDto> freeShippingRulesDtoPageDTO = freeShippingRulesService.queryPage(freeShippingRulesQuery);
        return JSONResult.success("查询成功",freeShippingRulesDtoPageDTO);
    }

    /**
     * 根据id查询包邮规则表信息
     * @param ruleId
     * @return
     */
    @GetMapping("query/{ruleId}")
    @ApiOperation(value = "根据id查询包邮规则表信息",notes = "根据id查询包邮规则表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("ruleId")Long ruleId){
        //1.根据id查询用户信息
        if (ruleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.RULEID_NOT_ID);
        }
        FreeShippingRules freeShippingRules = freeShippingRulesService.getById(ruleId);
        return JSONResult.success("查询成功",freeShippingRules);
    }

    /**
     * 根据id查询包邮规则表信息
     * @param ruleId
     * @return
     */
    @GetMapping("queryFreeShippingRulesById/{ruleId}")
    @ApiOperation(value = "根据id查询包邮规则表信息",notes = "根据id查询包邮规则表信息",httpMethod = "GET")
    public JSONResult queryFreeShippingRulesById(@PathVariable("ruleId")Long ruleId){
        //1.根据id查询用户信息
        if (ruleId == null){
            throw new GlobalCustomerException(ErrorEnumCode.RULEID_NOT_ID);
        }
        FreeShippingRules freeShippingRules = freeShippingRulesService.queryFreeShippingRulesById(ruleId);
        return JSONResult.success("查询成功",freeShippingRules);
    }

    /**
     * 查询全部包邮规则表信息
     * @param
     * @return
     */
    @GetMapping("queryFreeShippingRulesList")
    @ApiOperation(value = "查询全部包邮规则表信息",notes = "查询全部包邮规则表信息",httpMethod = "GET")
    public JSONResult queryFreeShippingRulesList(){
        List<FreeShippingRules> freeShippingRules = freeShippingRulesService.queryFreeShippingRulesList();
        return JSONResult.success("查询成功",freeShippingRules);
    }

    /**
     * 新增包邮规则表
     * @param freeShippingRulesDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增包邮规则表",notes = "新增包邮规则表",httpMethod = "POST")
    public JSONResult insert(@RequestBody FreeShippingRulesDto freeShippingRulesDto){
        freeShippingRulesService.insert(freeShippingRulesDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改包邮规则表信息
     * @param freeShippingRulesDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改包邮规则表信息",notes = "修改包邮规则表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody FreeShippingRulesDto freeShippingRulesDto){
        freeShippingRulesService.update(freeShippingRulesDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除包邮规则表信息
     * @param ruleId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除包邮规则表信息",notes = "删除包邮规则表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("ruleId") Long ruleId) {
        freeShippingRulesService.delete(ruleId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除包邮规则表信息
     * @param ruleIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除包邮规则表信息",notes = "批量删除包邮规则表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> ruleIds) {
        freeShippingRulesService.deleteBatch(ruleIds);
        return JSONResult.success("批量删除成功");
    }
}
